package net.liteheaven.xblib.common.io;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
 * Created by liangy on 2017/2/10.
 */
public class SPHelper {

    public static void putInt(Context ctx, String file, String key, int value){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static void putLong(Context ctx, String file, String key, long value){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static void putBoolean(Context ctx, String file, String key, boolean value){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static void putString(Context ctx, String file, String key, String value){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static int getInt(Context ctx, String file, String key, int def){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        return sp.getInt(key, def);
    }

    public static long getLong(Context ctx, String file, String key, long def){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        return sp.getLong(key, def);
    }

    public static boolean getBoolean(Context ctx, String file, String key, boolean def){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        return sp.getBoolean(key, def);
    }

    public static String getString(Context ctx, String file, String key, String def){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        return sp.getString(key, def);
    }

    public static void putValues(Context ctx, String file, Map<String, ?> items){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        for (Map.Entry<String, ?> item : items.entrySet()){
            String key = item.getKey();
            Object value = item.getValue();
            if (value instanceof Integer){
                editor.putInt(key, (Integer)value);
            }else if (value instanceof Boolean){
                editor.putBoolean(key, (Boolean)value);
            }else if (value instanceof String){
                editor.putString(key, (String)value);
            }else if (value instanceof Long){
                editor.putLong(key, (Long)value);
            }
        }
        editor.apply();
    }

    public static Map<String, ?> getAll(Context ctx, String file){
        SharedPreferences sp = ctx.getSharedPreferences(file, Context.MODE_PRIVATE);
        return sp.getAll();
    }

}
